<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='编辑评论',active='comments'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">编辑评论</h4>
                    </div>
                    <div class="col-md-12">
                        <form id="commentForm" method="post">
                            <input type="hidden" name="id" th:value="${comment.id}"/>
                            <input type="hidden" name="articleId" th:value="${comment.articleId}"/>
                            <div class="form-group">
                                <label for="content">评论内容</label>
                                <textarea class="form-control" id="content" name="content" rows="5" th:text="${comment.content}" required></textarea>
                            </div>
                            <button type="button" class="btn btn-primary" onclick="updateComment()">更新评论</button>
                            <a th:href="@{/admin/comments}" class="btn btn-default">取消</a>
                        </form>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>
<script type="text/javascript">
    function updateComment() {
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        $.ajax({
            type: 'post',
            url: '/admin/comment/update',
            data: $('#commentForm').serialize(),
            dataType: 'json',
            beforeSend: function (xhr) {
                xhr.setRequestHeader(header, token);
            },
            success: function (result) {
                if (result && result.success) {
                    window.alert("评论更新成功");
                    window.location.href = '/admin/comments';
                } else {
                    window.alert(result.msg || '评论更新失败');
                }
            }
        });
    }
</script>
</body>
</html>